package org.gd.rest.jersey.client;

import com.sun.jersey.api.client.ClientResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class AcquiringClient {

    // http://10.42.8.168:8080

    private static final String PROTOCOL = "http";
    private static final String HOST = "localhost";//"10.42.14.143";
    private static final Integer PORT = 8000;
    private static final String APP = "Acquiring";
    private static final String PATH = String.format("%s://%s:%d/%s",
            PROTOCOL, HOST, PORT, APP);

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executor = Executors.newFixedThreadPool(8);
        List<Future<ClientResponse>> futures = new ArrayList<>();
        CountDownLatch latch = new CountDownLatch(1);
        for(int i=0; i<1; i++) {
            futures.add(executor.submit(new AcquiringCreateCallable(
                    getContent(),
                    PATH + "/public/order/create",
                    latch)));
        }
        latch.countDown();
        executor.shutdown();
        while (!executor.isTerminated());


//
//
//        long t = System.currentTimeMillis();
//        Client client = Client.create();
//        WebResource webResource = client
//                .resource(PATH + "/order/0");
//        ClientResponse response = webResource
//                .accept(MediaType.TEXT_XML)
//                .get(ClientResponse.class);
//        if (response.getStatus() != 200) {
//            System.err.println("Failed : HTTP error code : " + response.getStatus());
//        }
//        String output = response.getEntity(String.class);
//        System.out.printf("Output from Server .... %s (t: %s ms)\n",
//                output,
//                System.currentTimeMillis() - t);
//
//
//
//
//
//        t = System.currentTimeMillis();
//        client = Client.create();
//        webResource = client
//                .resource(PATH + "/order/3");
//        response = webResource
//                .accept(MediaType.TEXT_XML)
//                .get(ClientResponse.class);
//        if (response.getStatus() != 200) {
//            System.err.println("Failed : HTTP error code : " + response.getStatus());
//        }
//        output = response.getEntity(String.class);
//        System.out.printf("Output from Server .... %s (t: %s ms)\n",
//                output,
//                System.currentTimeMillis() - t);




//
//
//
//        t = System.currentTimeMillis();
//        client = Client.create();
//        webResource = client
//                .resource(PATH + "/user/9223372036854775807/orders");
//        response = webResource
//                .accept(MediaType.TEXT_XML)
//                .get(ClientResponse.class);
//        if (response.getStatus() != 200) {
//            System.err.println("Failed : HTTP error code : " + response.getStatus());
//        }
//        output = response.getEntity(String.class);
//        System.out.printf("Output from Server .... %s (t: %s ms)\n",
//                output,
//                System.currentTimeMillis() - t);
//
//






    }

    private static String getContent() {
        return  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".trim() +
                "<order sourceOrder=\"PRIVAT_24\">".trim() +
                "    <user>".trim() +
                "        <ekbId>90</ekbId>".trim() +
                "        <name>${name}</name>".trim() +
                "        <comment>${comment}</comment>".trim() +
                "        <okpo>${okpo}</okpo>".trim() +
                "        <mfo>${mfo}</mfo>".trim() +
                "        <juridicalPersonPhone>${juridicalPersonPhone}</juridicalPersonPhone>".trim() +
                "        <juridicalPersonMail>${juridicalPersonMail}</juridicalPersonMail>".trim() +
                "        <juridicalPersonDirectorName>${juridicalPersonDirectorName}</juridicalPersonDirectorName>".trim() +
                "        <juridicalPersonDirectorPhone>${juridicalPersonDirectorPhone}</juridicalPersonDirectorPhone>".trim() +
                "        <ip>${ip}</ip>".trim() +
                "    </user>".trim() +
                "    <enterprise>".trim() +
                "        <jurName>${jurName}</jurName>".trim() +
                "        <shortJurName>${shortJurName}</shortJurName>".trim() +
                "        <jurAddress>${jurAddress}</jurAddress>".trim() +
                "    </enterprise>".trim() +
                "    <retailers>".trim() +
                "        <retailer terminalCurrency=\"${terminalCurrency}\">".trim() +
                "            <city>${city}</city>".trim() +
                "            <street>${street}</street>".trim() +
                "            <build>${build}</build>".trim() +
                "            <branch>${branch}</branch>".trim() +
                "            <terminalCount>71</terminalCount>".trim() +
                "            <bankContacts>".trim() +
                "                <attendantLDAP>${attendantLDAP}</attendantLDAP>".trim() +
                "                <communicantLDAP>${communicantLDAP}</communicantLDAP>".trim() +
                "            </bankContacts>".trim() +
                "            <trade>".trim() +
                "                <name>${name}</name>".trim() +
                "                <email>${email}</email>".trim() +
                "                <phone>${phone}</phone>".trim() +
                "                <translateName>${translateName}</translateName>".trim() +
                "                <nameForTerminal>${nameForTerminal}</nameForTerminal>".trim() +
                "                <responsible name=\"${name}\" phone=\"${phone}\" email=\"${email}\"/>".trim() +
                "                <account>${account}</account>".trim() +
                "                <accountCurrency>${accountCurrency}</accountCurrency>".trim() +
                "                <mcc>53</mcc>".trim() +
                "                <assortment>${assortment}</assortment>".trim() +
                "                <advancedOptions>".trim() +
                "                    <cashFlow>MORE_THAN_50</cashFlow>".trim() +
                "                    <tradeArea>MORE_THAN_200</tradeArea>".trim() +
                "                    <privatBankVIP infiniteSignia=\"81.96\" platinum=\"47.68\" gold=\"28.39\" lady=\"true\" vipClub=\"true\" vipBirthday=\"true\"/>".trim() +
                "                    <corporateInformation merchant=\"${merchant}\"/>".trim() +
                "                </advancedOptions>".trim() +
                "            </trade>".trim() +
                "            <acquiringGroup>".trim() +
                "                <installmentByCards>true</installmentByCards>".trim() +
                "                <auto>false</auto>".trim() +
                "                <educ>true</educ>".trim() +
                "                <build>true</build>".trim() +
                "                <fuelCard>true</fuelCard>".trim() +
                "                <distribution>true</distribution>".trim() +
                "                <contactless>true</contactless>".trim() +
                "                <tNight>false</tNight>".trim() +
                "            </acquiringGroup>".trim() +
                "            <permittedOperations>".trim() +
                "                <sale>false</sale>".trim() +
                "                <preAuthorization>false</preAuthorization>".trim() +
                "                <refund>true</refund>".trim() +
                "                <saleCompletion>true</saleCompletion>".trim() +
                "                <annulment>false</annulment>".trim() +
                "                <manualInput>true</manualInput>".trim() +
                "                <cashBack>false</cashBack>".trim() +
                "                <saleCompletionManually>true</saleCompletionManually>".trim() +
                "                <remotePayment>false</remotePayment>".trim() +
                "                <manualCardInput>true</manualCardInput>".trim() +
                "            </permittedOperations>".trim() +
                "        </retailer>".trim() +
                "        <retailer terminalCurrency=\"${terminalCurrency}\">".trim() +
                "            <city>${city}</city>".trim() +
                "            <street>${street}</street>".trim() +
                "            <build>${build}</build>".trim() +
                "            <branch>${branch}</branch>".trim() +
                "            <terminalCount>73</terminalCount>".trim() +
                "            <bankContacts>".trim() +
                "                <attendantLDAP>${attendantLDAP}</attendantLDAP>".trim() +
                "                <communicantLDAP>${communicantLDAP}</communicantLDAP>".trim() +
                "            </bankContacts>".trim() +
                "            <trade>".trim() +
                "                <name>${name}</name>".trim() +
                "                <email>${email}</email>".trim() +
                "                <phone>${phone}</phone>".trim() +
                "                <translateName>${translateName}</translateName>".trim() +
                "                <nameForTerminal>${nameForTerminal}</nameForTerminal>".trim() +
                "                <responsible name=\"${name}\" phone=\"${phone}\" email=\"${email}\"/>".trim() +
                "                <account>${account}</account>".trim() +
                "                <accountCurrency>${accountCurrency}</accountCurrency>".trim() +
                "                <mcc>84</mcc>".trim() +
                "                <assortment>${assortment}</assortment>".trim() +
                "                <advancedOptions>".trim() +
                "                    <cashFlow>LESS_THAN_50</cashFlow>".trim() +
                "                    <tradeArea>MORE_THAN_200</tradeArea>".trim() +
                "                    <privatBankVIP infiniteSignia=\"70.32\" platinum=\"48.08\" gold=\"7.16\" lady=\"true\" vipClub=\"true\" vipBirthday=\"true\"/>".trim() +
                "                    <corporateInformation merchant=\"${merchant}\"/>".trim() +
                "                </advancedOptions>".trim() +
                "            </trade>".trim() +
                "            <acquiringGroup>".trim() +
                "                <installmentByCards>false</installmentByCards>".trim() +
                "                <auto>false</auto>".trim() +
                "                <educ>false</educ>".trim() +
                "                <build>false</build>".trim() +
                "                <fuelCard>false</fuelCard>".trim() +
                "                <distribution>true</distribution>".trim() +
                "                <contactless>false</contactless>".trim() +
                "                <tNight>true</tNight>".trim() +
                "            </acquiringGroup>".trim() +
                "            <permittedOperations>".trim() +
                "                <sale>false</sale>".trim() +
                "                <preAuthorization>true</preAuthorization>".trim() +
                "                <refund>false</refund>".trim() +
                "                <saleCompletion>true</saleCompletion>".trim() +
                "                <annulment>false</annulment>".trim() +
                "                <manualInput>false</manualInput>".trim() +
                "                <cashBack>true</cashBack>".trim() +
                "                <saleCompletionManually>false</saleCompletionManually>".trim() +
                "                <remotePayment>true</remotePayment>".trim() +
                "                <manualCardInput>false</manualCardInput>".trim() +
                "            </permittedOperations>".trim() +
                "        </retailer>".trim() +
                "    </retailers>".trim() +
                "</order>";
    }
}
